package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.*;
import com.bwie.pojo.*;
import com.bwie.service.TbUserService;
import com.bwie.utils.MobileMessageUtil;
import com.bwie.utils.Result;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.PageInfoVo;
import com.bwie.vo.PpVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author Yoo19
* @description 针对表【tb_user(用户表)】的数据库操作Service实现
* @createDate 2025-06-17 09:15:41
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{
    @Autowired
    TbUserMapper tbUserMapper;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    TbCustMapper tbCustMapper;
    @Autowired
    TbDictMapper tbDictMapper;
    @Autowired
    TbRoleMapper tbRoleMapper;
    @Autowired
    TbCustRecordMapper tbCustRecordMapper;

    @Override
    public Result sendCode(String mobile) {
       // 5.	用户发送验证码若未注册则是直接进行注册，如果是注册用户则直接进行登录操作
        TbUser user = tbUserMapper.selectOne(new LambdaQueryWrapper<TbUser>().eq(TbUser::getUserMobile, mobile));
        if(user == null){
            TbUser tbUser = new TbUser();
            tbUser.setUserMobile(mobile);

            tbUserMapper.insert(tbUser);
        }
        //生成验证码
        String code = RandomUtil.randomNumbers(4);
        System.out.println("验证码为：" + code);
        //存入redis
        String codeKey = "login-" + mobile;
        redisTemplate.opsForValue().set(codeKey,code,2, TimeUnit.MINUTES);

        //发送消息
        MobileMessageUtil.sendMobileMessage(mobile,code);

        return Result.ok();
    }

    @Override
    public Result login(LoginInfoVo loginInfoVo) {
        //根据手机号查找用户
        TbUser user = tbUserMapper.selectOne(new LambdaQueryWrapper<TbUser>().eq(TbUser::getUserMobile, loginInfoVo.getUserMobile()));
        if(user == null ){
            return Result.error().setMsg("请注册");
        }
        //判段验证码是否正确
        String codeKey = "login-" + loginInfoVo.getUserMobile();
        String redisCode = redisTemplate.opsForValue().get(codeKey);
        if(redisCode == null){
            return Result.error().setMsg("验证码已过期");
        }
        if(!loginInfoVo.getUserCode().equals(redisCode)){
            return Result.error().setMsg("验证码错误");
        }

        //生成token
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", user.getUserId())
                .claim("nickName", user.getNickName())
                .compact();
        String redisKey = "token-" + user.getUserId();
        redisTemplate.opsForValue().set(redisKey,token,60,TimeUnit.MINUTES);


        user.setToken(token);

        return Result.ok().setData(user);
    }

    @Override
    public Result getCustList(PageInfoVo pageInfoVo) {
        Page<TbCust> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<TbCust> tbCustPage = tbCustMapper.getCustList(page,pageInfoVo);
        return Result.ok().setData(tbCustPage);
    }

    @Override
    public Result getDictList(Integer typeId) {
        //根据typeid获取对应数据
       List<TbDict> tbDicts =  tbDictMapper.getDictList(typeId);
        return Result.ok().setData(tbDicts);
    }

    @Override
    public Result addCust(TbCust tbCust) {
        //添加
        tbCustMapper.insert(tbCust);
        return Result.ok();
    }

    @Override
    public Result getCustDetail(Integer custId) {
        //根据custid查询客户信息
        List<TbCust> custList = tbCustMapper.getCustDetail(custId);
        return Result.ok().setData(custList);
    }

    @Override
    public Result getPeopleList(PpVo ppVo, Integer custId) {
        //根据custid查询客户联系人信息
        Page<TbUser> page = new Page<>(ppVo.getPageNum(), ppVo.getPageSize());
        Page<TbUser> tbUserPage = tbUserMapper.getPeopleList(page,custId);
        return Result.ok().setData(tbUserPage);
    }

    @Override
    public Result addUser(TbUser tbUser) {
        //添加联系人
        tbUserMapper.insert(tbUser);
        return Result.ok();
    }

    @Override
    public Result getUserRole() {
        //获取所有角色
        List<TbRole> tbUserRoles = tbRoleMapper.selectList(null);
        return Result.ok().setData(tbUserRoles);
    }

    @Override
    public Result getRecord(Integer custId) {
        //获取跟进记录
        List<TbCustRecord> tbCustRecordList = tbCustRecordMapper.getRecord(custId);
        return Result.ok().setData(tbCustRecordList);
    }

    @Override
    public Result addRecord(TbCustRecord tbCustRecord) {
        //跟进记录
        tbCustRecordMapper.insert(tbCustRecord);
        return Result.ok();
    }
}




